using Microsoft.Deployment.WindowsInstaller;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CustomActions
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CustomAction1(Session session)
        {
            try
            {
                // 获取安装参数
                string installDir = session.CustomActionData["INSTALLDIR"];
                string stationName = session.CustomActionData["STATION_NAME"];
                string phone = session.CustomActionData["PHONE"];
                string legal = session.CustomActionData["LEGAL"];
                string accountNumber = session.CustomActionData["ACCOUNT_NUMBER"];
                string accountPassword = session.CustomActionData["ACCOUNT_PASSWORD"];

                // 构建配置文件路径
                string configPath = Path.Combine(installDir, "config.ini");

                // 构建配置内容
                StringBuilder configContent = new StringBuilder();
                configContent.AppendLine("[app]");
                configContent.AppendLine($"station_name={stationName}");
                configContent.AppendLine($"phone={phone}");
                configContent.AppendLine($"legal={legal}");
                configContent.AppendLine($"account_number={accountNumber}");
                configContent.AppendLine($"account_password={accountPassword}");

                // 以UTF-8编码写入文件
                File.WriteAllText(configPath, configContent.ToString(), Encoding.UTF8);

                return ActionResult.Success;
            }
            catch (Exception ex)
            {
                session.Log($"Error creating config file: {ex.Message}");
                return ActionResult.Failure;
            }
        }
    }
}